package com.fenghongzhang.day008test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {
    private Button down;
    private Button read;
    private ImageView img;

    private static final String TAG = "MainActivity";

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 111 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            //执行业务逻辑.
        }else{
            //==
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        down = (Button) findViewById(R.id.down);
        read = (Button) findViewById(R.id.read);
        img = (ImageView) findViewById(R.id.img);


        //根据版本判断是否要动态授权
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},111);
        }else{
            //==
        }


    }

    //下载
    public void down(View view) {
        //开启异步
        new MyTask().execute("https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1141259048,554497535&fm=26&gp=0.jpg");

    }

    //从sd卡中读取
    public void read(View view) {
        File file = Environment.getExternalStorageDirectory();

        File file1 = new File(file, "la.jpg");

        Log.i(TAG, "read: "+file1.getAbsolutePath());
        //如果把一个文件变成一个图片.
        Bitmap bitmap = BitmapFactory.decodeFile(file1.getAbsolutePath());

        //给组件赋值即可
        img.setImageBitmap(bitmap);
    }


    //异步
    class MyTask extends AsyncTask<String,Void, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... strings) {

            try {
                URL url = new URL(strings[0]);//png

                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                if(connection.getResponseCode() == 200){
                    InputStream inputStream = connection.getInputStream();
                    //写入到sd卡. 有一个文件路径.
                    File file = Environment.getExternalStorageDirectory();

                    //输出流
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(file, "la.jpg"));
                    byte[] b = new byte[1024];
                    int len = 0;

                    //根据流不停的写
                    while((len = inputStream.read(b)) != -1){
                        fileOutputStream.write(b,0,len);
                        fileOutputStream.flush();
                    }

                    fileOutputStream.close();
                    inputStream.close();
                }




            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }

}
